package work.jame.blog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import work.jame.blog.configuration.security.LoginUser;
import work.jame.blog.framework.constant.UrlConstant;
import work.jame.blog.framework.constant.UserConfigurationConstant;
import work.jame.blog.framework.exception.db.DataNotExistException;
import work.jame.blog.framework.exception.db.InsertDataBaseException;
import work.jame.blog.framework.utils.EmailUtils;
import work.jame.blog.framework.utils.ThreadPoolUtil;
import work.jame.blog.framework.utils.security.SpringSecurityUtils;
import work.jame.blog.pojo.Comment;
import work.jame.blog.pojo.User;
import work.jame.blog.pojo.UserCommentCall;
import work.jame.blog.mapper.UserCommentCallMapper;
import work.jame.blog.pojo.UserConfiguration;
import work.jame.blog.pojo.dto.CommentDTO;
import work.jame.blog.service.UserCommentCallService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import work.jame.blog.service.UserConfigurationService;
import work.jame.blog.service.UserMessageService;
import work.jame.blog.service.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jame
 * @since 2023-05-20
 */
@Service
public class UserCommentCallServiceImpl extends ServiceImpl<UserCommentCallMapper, UserCommentCall> implements UserCommentCallService {

    @Autowired
    private UserConfigurationService userConfigurationService;

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public boolean saveUserCommentCall(CommentDTO commentDTO) {
        List<Long> callUserIds = commentDTO.getCallUserIds();
        if (Objects.isNull(callUserIds) || callUserIds.isEmpty()) {
            return true;
        }
        LoginUser loginUser = SpringSecurityUtils.getCurrentLoginUserInfo();
        User user = loginUser.getUser();
        Long userId = user.getId();
        Comment comment = commentDTO.getComment();

        ArrayList<UserCommentCall> saveList = new ArrayList<>();
        for (Long callUserId : callUserIds) {
            UserCommentCall call = new UserCommentCall();
            call.setSendCommentUserId(userId);
            call.setCommentId(comment.getId());
            call.setCallUserId(callUserId);
            saveList.add(call);
            UserConfiguration userConfiguration = userConfigurationService.queryUserConfigurationByUserId(callUserId);
            Integer callMeOperation = userConfiguration.getCallMeOperation();
            if (Objects.isNull(callMeOperation)) {
                throw new RuntimeException("没有找到对应用户@操作");
            }
            if (callMeOperation == UserConfigurationConstant.CALL_ME_NOTING) {
                //do noting
            } else {
                String userNickName = SpringSecurityUtils.getCurrentLoginUserInfo().getUser().getNickName();
                String messageContent = userNickName + "在评论中@了你 : " + comment.getContent();
                if (callMeOperation >= UserConfigurationConstant.CALL_ME_SEND_MESSAGE) {
                    boolean sendCallMessage = userMessageService.sendCallMessageToUser(userId,
                            callUserId,
                            messageContent,
                            UrlConstant.ARTICLE_DETAIL_URL + comment.getArticleId());
                    if (!sendCallMessage) {
                        throw new InsertDataBaseException("插入@用户消息失败");
                    }
                }
                if (callMeOperation == UserConfigurationConstant.CALL_ME_SEND_MESSAGE_AND_EMAIL) {
                    User receiveUser = userService.getById(callUserId);
                    if (Objects.isNull(receiveUser)) {
                        throw new DataNotExistException("获取@的用户信息为空");
                    }
                    String receiveUserEmail = receiveUser.getEmail();
                    ThreadPoolUtil.execute(() -> {
                        EmailUtils.sendCallUserMessage(receiveUserEmail, receiveUser.getNickName(), messageContent);
                    });
                }
            }

        }
        return this.saveBatch(saveList);
    }
}
